/* my super cycle include
 * mSC.h
 *
 *  Created on: 2016-9-30
 *      Author: nkj
 */

#ifndef MSC_H_
#define MSC_H_

#include "stdint.h"
#include "stdlib.h"
#include "mSCConfig.h"

#if 0
typedef signed char   uint8;
typedef unsigned char uint8_t;

typedef signed short   uint16;
typedef unsigned short uint16_t;

typedef signed int   uint32;
typedef unsigned int uint32_t;

typedef signed long   uint64;
typedef unsigned long uint64_t;
#endif

typedef enum _mscStatus
{
	MSC_STOP = 0,
	MSC_START
}emscStatus;

typedef enum _mscTaskStatus
{
	MSC_TASK_INVALID = 0,	//无效，不运行;谁触发的谁更改
	MSC_TASK_EFFECTIVE,		//任务有效
	MSC_TASK_ZERODELAY,		//0延迟运行
	MSC_TASK_MSDELAY,		//ms延迟运行
	MSC_TASK_PEND,			//暂停，可以被触发更改为以上2个
}emscTaskStatus;

typedef enum _errorcode
{
	MSC_SUCCESS = 0,	//成功
	MSC_FAIL,			//失败
	MSC_TASKNUMISMAX,	//任务数达到上限
	MSC_NOTHISTASK,		//没有或没注册这个任务
	MSC_TASKINVALID,	//任务已失效
}emscerrorcode;

#define pdMSC_US_TO_TICKS(t)	(((t) * MSCOREFREQ) / (1000000))
#define pdMSC_MS_TO_TICKS(t)	(((t) * MSCOREFREQ) / (1000))
#define pdMSC_S_TO_TICKS(t)		(((t) * MSCOREFREQ))

#define pdMSC_TICKS_TO_US(t)	(((t) * (1000000) / MSCOREFREQ))
#define pdMSC_TICKS_TO_MS(t)	(((t) * (1000) / MSCOREFREQ))
#define pdMSC_TICKS_TO_S(t)		(((t) / MSCOREFREQ))


void mscInit(void);
void mscStart(void);
void mscStop(void);
emscStatus getMSCSta(void);
uint32_t getMSCFreq(void);
uint32_t getMSCRunTime(void);

emscerrorcode mscCreateTask(emscTaskStatus tasksta,uint32_t periodic,uint32_t period,void *pPram,void (*handler));

emscerrorcode mscDeleteTask(void (*handler));
emscerrorcode mscPendTask(void (*handler));
emscerrorcode mscRecoverTaskToZeroDelay(void (*handler));
emscerrorcode mscRecoverTaskToMsDelay(void (*handler));

emscerrorcode mscGetTaskSta(emscTaskStatus *sta,void (*handler));
emscerrorcode mscGetTaskPeriod(uint32_t *period,void (*handler));
emscerrorcode mscGetTaskTimeKeeping(uint32_t *periodic,void (*handler));
emscerrorcode mscGetTaskTimeSurplus(uint32_t *surplus,void (*handler));
emscerrorcode mscGetTaskPram(void *pram,void (*handler));
emscerrorcode mscGetTaskTimeGap(uint32_t *timegap,void (*handler));

emscerrorcode mscSetTaskSta(emscTaskStatus sta,void (*handler));
emscerrorcode mscSetTaskPeriod(uint32_t period,void (*handler));
emscerrorcode mscSetTaskTimeKeeping(uint32_t time,void (*handler));
emscerrorcode mscSetTaskPram(void *pram,void (*handler));

emscerrorcode mscRecoverTaskToZeroDelayFromIRQ(void (*handler));
emscerrorcode mscRecoverTaskToMsDelayFromIRQ(void (*handler));
emscerrorcode mscSetTaskStaFromIRQ(emscTaskStatus sta,void (*handler));

void mscMsDelay(uint32_t ms);

#endif /* MSC_H_ */
